<?php
//require_once libfile('function/renren');

class renren_api {
	var $params = array('version' => '1.0',
						'format' => 'JSON');
	var $secret;
	var $api_server = 'http://api.renren.com/restserver.do?';
	
	function &instance() {
		static $object;
		if(empty($object)) {
			$object = new renren_api();
		}
		return $object;
	}

	function renren_api() {
		global $_G;
		loadcache('plugin');
		$this->secret = $_G['cache']['plugin']['renren']['secret'];
		$this->params['api_key'] = $_G['cache']['plugin']['renren']['api_key'];
	}
	
	function get_user_info() {
		$params = array('fields'=>array('uid', 'name','sex','birthday', 'mainurl','hometown_location', 'work_history', 'university_history', 'tinyurl', 'headurl', 'headurl_with_logo', 'tinyurl_with_logo', 'mainurl_with_logo'));
		$params['session_key'] = rr_get_cookie('session_key');
		$result = $this->call_api('users.getInfo', $params);
		return $result[0];
	}       
	function get_user_info_without_session($uid) {
                $params = array('uids'=>$uid);
                $user_info= $this->call_api('users.getInfo', $params);
                $result=$user_info[0];
                $username=$result->name;
                if($username)
                {
                        $result->name=iconv("utf-8", "gbk",$username);
                }
                return $result;
        }
	function get_user_friend() {
		$params = array();
		$params['session_key'] = rr_get_cookie('session_key');
		$result = $this->call_api('friends.getFriends', $params);
		return $result;
	}       
	
	function call_api($method, $params) {
		if(!empty($params) && is_array($params)) {
			foreach($params as $k => $v) {
				$this->params[$k] = $v;
			}
		}
		$this->params['method'] = $method;
		
		$post_body = $this->create_post_body();
		
		if (function_exists('curl_init')) {
		  $request = curl_init();
		  curl_setopt($request, CURLOPT_URL, $this->api_server);
		  curl_setopt($request, CURLOPT_POST, 1);
		  curl_setopt($request, CURLOPT_POSTFIELDS, $post_body);
		  curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
		  
		  $result = curl_exec($request);
		  
		  curl_close($request);
		} else {
			$context =array('http' => array('method' => 'POST',
											'header' => 'Content-type: application/x-www-form-urlencoded'."\r\n".
														'User-Agent: Facebook API PHP5 Client 1.1 '."\r\n".
														'Content-length: ' . strlen($post_body),
											'content' => $post_body));
			$contextid=stream_context_create($context);
			$sock=fopen($this->api_server, 'r', false, $contextid);
			if ($sock) {
				$result='';
				while (!feof($sock))
				  $result.=fgets($sock, 4096);
				
				fclose($sock);
			}
		}
		
		$result = $this->rr_handle_response($result);
		
		return $result;
	}
	
	function create_post_body() {
		$this->params['call_id'] = time();
		
		$post_params = array();
		
		foreach ($this->params as $key => &$val) {
		  if(is_array($val)) 
		  	$val = implode(',', $val);
		  $post_params[] = $key.'='.urlencode($val);
		}
		
		$post_params[] = 'sig='.rr_generate_sig($this->params, $this->secret);
		return implode('&', $post_params);
	}
	
	function rr_handle_response($result) {
		$array = json_decode($result);
		$this->rr_check_response($array);
		
		return $array;
	}
	
	function rr_check_response(&$result) {
		$msg='';
		if($result['error_code'])
		{
			$msg .= '<br>'.lang('error_visit', 'renren').'<br>';
			if($result['error_code'][0])
			{
				$msg .= lang('error_code', 'renren').':'.$result['error_code'][0].'<br>';
			}
			if($result['error_msg'][0])
			{
				$msg .= lang('error_code', 'renren').':'.$result['error_msg'][0].'<br>';
			}
		}
		
		if($msg!='' && $result['error_code'][0]!='10702' && $result['error_code'][0]!='10600' ){
			echo $msg;
			exit;
		}
	}
}

?>
